package com.video4s.manager;

import java.util.ArrayList;

import org.apache.log4j.Logger;

import com.video4s.dao.UserYoutubeDAO;
import com.video4s.dao.UserYoutubeImplDAO;
import com.video4s.entity.UserYoutubeEntity;
import com.video4s.utils.PropetiesUtils;

public class StartCrawler {

	private static UserYoutubeDAO userYoutubeDAO = new UserYoutubeImplDAO();
	private static Logger logger = Logger.getLogger(StartCrawler.class);
	private static int SIZE_QUEUE = 50;
	private static int THREAD = 6;
	private static int SLEEP_LINK = 30000;
	public StartCrawler() {
		// TODO Auto-generated constructor stub
	}

	public void startThreadDB() {
		Thread thread1 = new Thread(new ThreadQueueDBUser());
		Thread thread2 = new Thread(new ThreadQueueDBVideo());
		thread1.start();
		thread2.start();
	}

	public void putLinkToQueue() {
		try {
			ArrayList<UserYoutubeEntity> list = new ArrayList<UserYoutubeEntity>();
			if (ManagerQueue.concurrentLinkedQueueLink.size() == 0) {
				list = userYoutubeDAO.onSelectTop(SIZE_QUEUE, 0);
				for (UserYoutubeEntity userYoutubeEntity2 : list) {
					ManagerQueue.concurrentLinkedQueueLink
							.add(userYoutubeEntity2);
				}
			} else {
				if (ManagerQueue.concurrentLinkedQueueLink.size() < SIZE_QUEUE) {
					int top = SIZE_QUEUE - ManagerQueue.concurrentLinkedQueueLink
							.size();
					list = userYoutubeDAO.onSelectTop(top, 0);
					for (UserYoutubeEntity userYoutubeEntity2 : list) {
						ManagerQueue.concurrentLinkedQueueLink
								.add(userYoutubeEntity2);
					}
				}
			}
			logger.info("## CONCURRENTLINKED QUEUE LINK SIZE: "+ManagerQueue.concurrentLinkedQueueLink.size());
			logger.info("## CONCURRENTLINKED QUEUE USER SIZE: "+ManagerQueue.concurrentLinkedQueueUserYoutube.size());
			logger.info("## CONCURRENTLINKED QUEUE VIDEO SIZE: "+ManagerQueue.concurrentLinkedQueueVideo.size());
			logger.info("## THREAD ACTIVE SIZE: "+Thread.activeCount());
		} catch (Exception e) {
			logger.info("## ERROR QUEUE: ", e);
		}
	}

	public void startThreadLinkQueue() {
		
		while (true) {
			try {
				SIZE_QUEUE = Integer.parseInt(PropetiesUtils.loadValue("SIZE_QUEUE_LINK"));
				THREAD = Integer.parseInt(PropetiesUtils.loadValue("THREAD_LINK"));
				SLEEP_LINK = Integer.parseInt(PropetiesUtils.loadValue("SLEEP_LINK"));
			} catch (Exception e) {
				logger.info("## ERROR LOAD CONFIG",e);
				break;
			}
			try {
				putLinkToQueue();
				int size = 0;
				if (Thread.activeCount() == 1) {
					size = THREAD;
				} else {
					size = (THREAD+1) - Thread.activeCount();
				}
				for (int i = 0; i < size; i++) {
					UserYoutubeEntity userYoutubeEntity = ManagerQueue.concurrentLinkedQueueLink.poll();
					Thread t = new Thread(new ThreadQueueLink(userYoutubeEntity));
					t.start();
				}
				Thread.sleep(SLEEP_LINK);
			} catch (Exception e) {
				logger.info("## ERROR THREAD LINK QUEUE: ", e);
			}
		}
	}

	public static void main(String[] args) {
		StartCrawler startCrawler = new StartCrawler();
		startCrawler.startThreadDB();
		startCrawler.startThreadLinkQueue();
		
	}

}
